package com.zxy.ziems.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.btp.common.exception.ConditionException;
import com.zxy.ziems.server.domain.ChannelField;
import com.zxy.ziems.server.dto.ChannelFieldDto;
import com.zxy.ziems.server.mapper.ChannelFieldMapper;
import com.zxy.ziems.server.service.ChannelFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName ChannelFieldServiceImpl
 * @Description
 * @Author 邓伟
 * @Date 2023/11/29 14:12
 * @Version 1.0
 */
@Service
public class ChannelFieldServiceImpl extends ServiceImpl<ChannelFieldMapper, ChannelField> implements ChannelFieldService {

    @Autowired
    private ChannelFieldMapper channelFieldMapper;

    @Override
    public void batchInsert(String channelId, List<ChannelFieldDto> channelParamList, List<ChannelFieldDto> pointParamList) {
        //先删除旧数据
        QueryWrapper<ChannelField> query = new QueryWrapper<>();
        query.lambda().eq(ChannelField::getChannelId,channelId);
        channelFieldMapper.delete(query);
        List<ChannelField> list = new ArrayList<>();
        Set<String> codeSet = new HashSet<>();
        Set<String> nameSet = new HashSet<>();
        for (ChannelFieldDto channelFieldDto : channelParamList) {
            ChannelField channelField = new ChannelField();
            channelField.setChannelId(channelId);
            String fieldCode = channelFieldDto.getFieldCode();
            if (codeSet.contains(fieldCode)) {
                throw new ConditionException("存在重复值：{" + fieldCode + "}");
            }else {
                codeSet.add(fieldCode);
                channelField.setFieldCode(fieldCode);
            }

            String fieldName = channelFieldDto.getFieldName();
            if (nameSet.contains(fieldName)) {
                throw new ConditionException("存在重复值：{" + fieldName + "}");
            }else {
                nameSet.add(fieldName);
                channelField.setFieldName(fieldName);
            }
            channelField.setFieldType(0);
            list.add(channelField);
        }
        codeSet = new HashSet<>();
        nameSet = new HashSet<>();
        for (ChannelFieldDto pointFieldDto : pointParamList) {
            ChannelField channelField = new ChannelField();
            channelField.setChannelId(channelId);
            String fieldCode = pointFieldDto.getFieldCode();
            if (codeSet.contains(fieldCode)) {
                throw new ConditionException("存在重复值：{" + fieldCode + "}");
            }else {
                codeSet.add(fieldCode);
                channelField.setFieldCode(fieldCode);
            }

            String fieldName = pointFieldDto.getFieldName();
            if (nameSet.contains(fieldName)) {
                throw new ConditionException("存在重复值：{" + fieldName + "}");
            }else {
                nameSet.add(fieldName);
                channelField.setFieldName(fieldName);
            }
            channelField.setFieldType(1);
            list.add(channelField);
        }
        saveBatch(list);
    }
}
